package com.wushi.lenovo.asia5b.app_di.profile.presenter;

import com.cloudinary.Cloudinary;
import com.cloudinary.android.MediaManager;
import com.cloudinary.utils.ObjectUtils;
import com.mvp.base.BasePresenter;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.MineFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_http.TopupService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by zhangyuncai on 2019/1/22.
 */
public class MineFragmentPresenter extends BasePresenter<MineFragmentView> {

    ApiService apiService;
    TopupService topupService;

    @Inject
    public MineFragmentPresenter(ApiService apiService, TopupService topupService) {
        this.apiService = apiService;
        this.topupService = topupService;
    }

    public void updateMemberHead(String path) {
        Cloudinary cloudinary = MediaManager.get().getCloudinary();
        Disposable subscribe = Observable.just(path)
                .map(new Function<String, Map>() {
                    @Override
                    public Map apply(String s) throws Exception {
                        //{signature=04cae05e7d6b79e02feb3ad79a6e8e35d28b2ca9,
                        // format=png,
                        // resource_type=image,
                        // secure_url=https://res.cloudinary.com/wushiyi/image/upload/v1552356224/haha.png,
                        // created_at=2019-03-12T02:03:44Z,
                        // type=upload,
                        // version=1552356224,
                        // url=http://res.cloudinary.com/wushiyi/image/upload/v1552356224/haha.png,
                        // public_id=haha,
                        // tags=[],
                        // original_filename=Screenshot_2019-03-11-11-22-53-343_com.wushi.aisa5bnew1,
                        // bytes=227809,
                        // width=1080,
                        // etag=08575e50190c176b9afb20b32fdb5dbe,
                        // placeholder=false,
                        // height=2160}
                        Map map = cloudinary.uploader().upload(s, ObjectUtils.emptyMap());
                        return map;
                    }
                })
                .flatMap(new Function<Map, ObservableSource<ResultModel>>() {
                    @Override
                    public ObservableSource<ResultModel> apply(Map map) throws Exception {
                        String avatar_url = map.get("url").toString();
                        return apiService.updateMemberHead(UserInfo.getMemberId(), avatar_url);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resultModel -> {
                    //内存更新即可,缓存不更新,如果闪退进入APP,重新获取用户资料即可
                    UserInfo.getUserInfo().setAvatar_url("");
                }, throwable -> {
                    throwable.printStackTrace();
                });
        composite.add(subscribe);
    }

}